package com.jzo2o.customer.controller.consumer;

import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 *
 * @Author mr.wei
 * @Date 2025/1/17 下午4:28
 */
@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 地址相关接口")
public class AddressBookController {
    @Autowired
    private IAddressBookService addressBookService;

    /**
     * 地址查看分页
     *
     * @param addressBookPageQueryReqDTO
     * @return
     */
    @GetMapping("/page")
    public PageResult<AddressBookResDTO> addressBookPage (AddressBookPageQueryReqDTO addressBookPageQueryReqDTO) {
        return addressBookService.addressBookPage (addressBookPageQueryReqDTO);
    }

    /**
     * 新增地址
     *
     * @param addressBookUpsertReqDTO
     */
    @PostMapping
    public void saveAddressBook (@RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        addressBookService.saveAddressBook (addressBookUpsertReqDTO);
    }

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AddressBookResDTO getAddressBookById (@PathVariable Long id) {
        return addressBookService.getAddressBookById (id);
    }

    /**
     * 修改地址
     *
     * @param id
     * @param addressBookUpsertReqDTO
     */
    @PutMapping("/{id}")
    public void updateAddressBook (@PathVariable Long id, @RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        addressBookService.updateAddressBook (id, addressBookUpsertReqDTO);
    }

    /**
     * 批量删除地址
     *
     * @param ids
     */
    @DeleteMapping("/batch")
    public void deleteAddressBook (@RequestBody List<Long> ids) {
        addressBookService.removeByIds (ids);
    }

    @PutMapping("/default")
    //flag id
    public void setDefaultAddress (
            @RequestParam("flag") Integer flag,
            @RequestParam("id") Long id
    ) {
        addressBookService.setDefaultAddress (flag,id);
    }

    @GetMapping("/defaultAddress")
    public AddressBookResDTO getDefaultAddress(){
        return addressBookService.getDefaultAddress (UserContext.currentUserId ());
    }


}
